From b4e5a3f07bde749cf32e9b635b7c69acf5e08be0 Mon Sep 17 00:00:00 2001 From: parkrrrr Date: Fri, 22 Sep 2006 13:29:56 +0000 Subject: [PATCH] stop using read64 for doubles; fix .gpl heading/speed --- an1.c | 4 +-- axim_gpb.c | 5 ++-- defs.h | 5 ++++ delgpl.c | 20 +++++++++----- easygps.c | 11 +++----- gbfile.c | 9 ++----- gtm.c | 6 ++--- mapsource.c | 76 ++++++++++++++++++++++++----------------------------- psp.c | 9 ++----- saroute.c | 2 +- tpg.c | 9 ++----- tpo.c | 9 ++----- util.c | 55 ++++++++++++++++++++++++++++---------- vitosmt.c | 10 ++----- 14 files changed, 116 insertions(+), 114 deletions(-) diff --git a/an1.c b/an1.c index 53b661138..c6483eb61 100644 --- a/an1.c +++ b/an1.c @@ -127,7 +127,7 @@ ReadDouble( FILE * f ) double tmp = 0; double result = 0; fread(&tmp, sizeof(tmp),1,f); - le_read64(&result, &tmp ); + result = le_read_double( &tmp ); return result; } @@ -135,7 +135,7 @@ static void WriteDouble(FILE * f, double d) { double tmp = 0; - le_read64( &tmp, (void *)&d ); + le_write_double( &tmp, d ); fwrite( &tmp, sizeof(tmp), 1, f ); } diff --git a/axim_gpb.c b/axim_gpb.c index 90fe552b5..c3383a085 100644 --- a/axim_gpb.c +++ b/axim_gpb.c @@ -67,9 +67,8 @@ decode_buff(const char *buff, route_head *track) tm.tm_hour = le_read16((void *) (buff + 24)); tm.tm_min = le_read16((void *) (buff + 26)); tm.tm_sec = le_read16((void *) (buff + 28)); - - le_read64(&lat, (void *) (buff + 32)); - le_read64(&lon, (void *) (buff + 40)); + lat = le_read_double( (void *) (buff + 32)); + lon = le_read_double( (void *) (buff + 40)); spd = le_read32_float((void *) (buff + 48)); dir = le_read32_float((void *) (buff + 52)); diff --git a/defs.h b/defs.h index 7b0d04c1b..c79cf9087 100644 --- a/defs.h +++ b/defs.h @@ -777,6 +777,11 @@ void le_write32(void *pp, const unsigned i); double pdb_read_double(void *p); void pdb_write_double(void *pp, double d); +double le_read_double(void *p); +double be_read_double(void *p); +void le_write_double(void *p, double d); +void be_write_double(void *p, double d); + /* * Prototypes for generic conversion routines (util.c). */ diff --git a/delgpl.c b/delgpl.c index a3c6cae40..ed326ef61 100644 --- a/delgpl.c +++ b/delgpl.c @@ -64,11 +64,15 @@ gpl_read(void) while (fread(&gp, sizeof(gp), 1, gplfile_in) > 0) { wpt_tmp = waypt_new(); - le_read64(&wpt_tmp->latitude, &gp.lat); - le_read64(&wpt_tmp->longitude, &gp.lon); - le_read64(&alt_feet, &gp.alt); + wpt_tmp->latitude = le_read_double(&gp.lat); + wpt_tmp->longitude = le_read_double(&gp.lon); + alt_feet = le_read_double(&gp.alt); wpt_tmp->altitude = FEET_TO_METERS(alt_feet); wpt_tmp->creation_time = le_read32(&gp.tm); + + wpt_tmp->course = le_read_double(&gp.heading); + wpt_tmp->speed = le_read_double(&gp.speed); + track_add_wpt(track_head, wpt_tmp); } } @@ -98,12 +102,16 @@ gpl_trackpt(const waypoint *wpt) double alt_feet = METERS_TO_FEET(wpt->altitude); int status = 3; gpl_point_t gp; + double speed = wpt->speed; + double heading = wpt->course; memset(&gp, 0, sizeof(gp)); le_write32(&gp.status, status); - le_read64(&gp.lat, &wpt->latitude); - le_read64(&gp.lon, &wpt->longitude); - le_read64(&gp.alt, &alt_feet); + le_write_double(&gp.lat, wpt->latitude); + le_write_double(&gp.lon, wpt->longitude); + le_write_double(&gp.alt, alt_feet ); + le_write_double(&gp.speed, speed ); + le_write_double(&gp.heading, heading ); le_write32(&gp.tm, wpt->creation_time); fwrite(&gp, sizeof(gp), 1, gplfile_out); diff --git a/easygps.c b/easygps.c index facfaf7c1..dd0269c91 100644 --- a/easygps.c +++ b/easygps.c @@ -96,7 +96,6 @@ data_read(void) char ibuf[10]; char bbuf[4096]; char *bbufp; - double d; do { unsigned char tag; waypoint *wpt_tmp; @@ -157,13 +156,11 @@ data_read(void) break; case 0x63: fread(ibuf, 8, 1, file_in); - le_read64(&d, ibuf); - wpt_tmp->latitude = d; + wpt_tmp->latitude = le_read_double(ibuf); break; case 0x64: fread(ibuf, 8, 1, file_in); - le_read64(&d, ibuf); - wpt_tmp->longitude = d; + wpt_tmp->longitude = le_read_double(ibuf); break; case 0x65: case 0x66: @@ -220,10 +217,10 @@ ez_disp(const waypoint *wpt) write_pstring(wpt->icon_descr); } fputc(0x63, file_out); - le_read64(tbuf, &wpt->latitude); + le_write_double(tbuf, wpt->latitude); fwrite(tbuf, 8, 1, file_out); fputc(0x64, file_out); - le_read64(tbuf, &wpt->longitude); + le_write_double(tbuf, wpt->longitude); fwrite(tbuf, 8, 1, file_out); if (wpt->notes) { fputc(5, file_out); diff --git a/gbfile.c b/gbfile.c index 8a457f917..f4773a3c4 100644 --- a/gbfile.c +++ b/gbfile.c @@ -477,12 +477,8 @@ double gbfgetdbl(gbfile *file) { char buf[8]; - double result; - gbfread(buf, 1, sizeof(buf), file); - le_read64(&result, buf); - - return result; + return le_read_double(buf); } float @@ -618,8 +614,7 @@ int gbfputdbl(const double d, gbfile *file) { char buf[8]; - - le_read64(buf, (char *)&d); + le_write_double(buf, d ); return gbfwrite(buf, 1, sizeof(buf), file); } diff --git a/gtm.c b/gtm.c index 981b0178a..379eb29a9 100644 --- a/gtm.c +++ b/gtm.c @@ -107,10 +107,8 @@ static double fread_double(FILE *fd) { char buf[8]; - double d; fread(buf, 8, 1, fd); - le_read64(&d, buf); - return d; + return le_read_double(buf); } static char * @@ -203,7 +201,7 @@ static void fwrite_double(FILE *fd, double val) { char buf[8]; - le_read64(buf, &val); + le_write_double(buf,val); fwrite(buf, 8, 1, fd); } diff --git a/mapsource.c b/mapsource.c index 08ab2f142..01e486426 100644 --- a/mapsource.c +++ b/mapsource.c @@ -97,26 +97,20 @@ arglist_t mps_args[] = { * A wrapper to ensure the doubles we fwrite are in correct endianness. */ -void -le_fwrite64(void *ptr, int sz, int ct, FILE *stream) +static void +le_fwrite_double(double d, FILE *stream) { unsigned char cbuf[8]; - - if ((sz != 8) || (ct != 1)) { - fatal(MYNAME ": Bad internal arguments to le_fwrite64.\n"); - } - - le_read64(cbuf, ptr); + le_write_double(cbuf,d); fwrite(cbuf, 8, 1, stream); } -void -le_fread64(void *ptr, int sz, int ct, FILE *stream) +static double +le_fread_double( FILE *stream) { unsigned char cbuf[8]; - fread(cbuf, 8, 1, stream); - le_read64(ptr, cbuf); + return le_read_double(cbuf); } static void @@ -536,22 +530,22 @@ mps_waypoint_r(FILE *mps_file, int mps_ver, waypoint **wpt, unsigned int *mpscla fread(tbuf, 1, 1, mps_file); /* altitude validity */ if (tbuf[0] == 1) { - le_fread64(&mps_altitude,sizeof(mps_altitude),1,mps_file); + mps_altitude = le_fread_double(mps_file); } else { mps_altitude = unknown_alt; - le_fread64(tbuf,sizeof(mps_altitude),1, mps_file); + fseek( mps_file, 8, SEEK_CUR ); } mps_readstr(mps_file, wptdesc, sizeof(wptdesc)); fread(tbuf, 1, 1, mps_file); /* proximity validity */ if (tbuf[0] == 1) { - le_fread64(&mps_proximity,sizeof(mps_proximity),1,mps_file); + mps_proximity = le_fread_double(mps_file); } else { mps_proximity = unknown_alt; - le_fread64(tbuf,sizeof(mps_proximity),1, mps_file); + fseek( mps_file, 8, SEEK_CUR ); } fread(tbuf, 4, 1, mps_file); /* display flag */ @@ -567,11 +561,11 @@ mps_waypoint_r(FILE *mps_file, int mps_ver, waypoint **wpt, unsigned int *mpscla fread(tbuf, 1, 1, mps_file); /* depth validity */ if (tbuf[0] == 1) { - le_fread64(&mps_depth,sizeof(mps_depth),1,mps_file); + mps_depth = le_fread_double( mps_file ); } else { mps_depth = unknown_alt; - le_fread64(tbuf,sizeof(mps_depth),1, mps_file); + fseek( mps_file, 8, SEEK_CUR ); } if ((mps_ver == 4) || (mps_ver == 5)) { @@ -704,7 +698,7 @@ mps_waypoint_w(FILE *mps_file, int mps_ver, const waypoint *wpt, const int isRou else { hdr[0] = 1; fwrite(hdr, 1 , 1, mps_file); - le_fwrite64(&mps_altitude, 8 , 1, mps_file); + le_fwrite_double( mps_altitude, mps_file ); } if (wpt->description) fputs(ascii_description, mps_file); fwrite(zbuf, 1, 1, mps_file); /* NULL termination */ @@ -717,7 +711,7 @@ mps_waypoint_w(FILE *mps_file, int mps_ver, const waypoint *wpt, const int isRou else { hdr[0] = 1; fwrite(hdr, 1 , 1, mps_file); - le_fwrite64(&mps_proximity, 8 , 1, mps_file); + le_fwrite_double( mps_proximity, mps_file ); } le_write32(&display, display); @@ -739,7 +733,7 @@ mps_waypoint_w(FILE *mps_file, int mps_ver, const waypoint *wpt, const int isRou else { hdr[0] = 1; fwrite(hdr, 1 , 1, mps_file); - le_fwrite64(&mps_depth, 8 , 1, mps_file); + le_fwrite_double(mps_depth, mps_file); } fwrite(zbuf, 2, 1, mps_file); /* unknown */ @@ -904,11 +898,11 @@ mps_route_r(FILE *mps_file, int mps_ver, route_head **rte) fread(tbuf, 1, 1, mps_file); /* altitude validity */ if (tbuf[0] == 1) { - le_fread64(&mps_altitude,sizeof(mps_altitude),1,mps_file); /* max alt of the whole route */ + mps_altitude = le_fread_double(mps_file); } else { mps_altitude = unknown_alt; - le_fread64(tbuf,sizeof(mps_altitude),1, mps_file); + fseek( mps_file, 8, SEEK_CUR ); } fread(&lat, 4, 1, mps_file); @@ -918,11 +912,11 @@ mps_route_r(FILE *mps_file, int mps_ver, route_head **rte) fread(tbuf, 1, 1, mps_file); /* altitude validity */ if (tbuf[0] == 1) { - le_fread64(&mps_altitude,sizeof(mps_altitude),1,mps_file); /* min alt of the whole route */ + mps_altitude = le_fread_double(mps_file); } else { mps_altitude = unknown_alt; - le_fread64(tbuf,sizeof(mps_altitude),1, mps_file); + fseek( mps_file, 8, SEEK_CUR ); } } @@ -1000,11 +994,11 @@ mps_route_r(FILE *mps_file, int mps_ver, route_head **rte) fread(tbuf, 1, 1, mps_file); /* altitude validity */ if (tbuf[0] == 1) { - le_fread64(&mps_altitude,sizeof(mps_altitude),1,mps_file); + mps_altitude = le_fread_double(mps_file); } else { mps_altitude = unknown_alt; - le_fread64(tbuf,sizeof(mps_altitude),1, mps_file); + fseek( mps_file, 8, SEEK_CUR ); } /* with MapSource routes, the real waypoint details are held as a separate waypoint, so copy from there @@ -1048,11 +1042,11 @@ mps_route_r(FILE *mps_file, int mps_ver, route_head **rte) fread(tbuf, 1, 1, mps_file); /* altitude validity */ if (tbuf[0] == 1) { - le_fread64(&mps_altitude,sizeof(mps_altitude),1,mps_file); + mps_altitude = le_fread_double( mps_file ); } else { mps_altitude = unknown_alt; - le_fread64(tbuf,sizeof(mps_altitude),1, mps_file); + fseek( mps_file, 8, SEEK_CUR ); } } @@ -1260,7 +1254,7 @@ mps_routehdr_w(FILE *mps_file, int mps_ver, const route_head *rte) else { hdr[0] = 1; fwrite(hdr, 1 , 1, mps_file); - le_fwrite64(&maxalt, 8 , 1, mps_file); + le_fwrite_double(maxalt, mps_file); } lat = GPS_Math_Deg_To_Semi(minlat); @@ -1279,7 +1273,7 @@ mps_routehdr_w(FILE *mps_file, int mps_ver, const route_head *rte) hdr[0] = 1; fwrite(hdr, 1 , 1, mps_file); - le_fwrite64(&minalt, 8 , 1, mps_file); + le_fwrite_double(minalt, mps_file); } le_write32(&rte_datapoints, rte_datapoints); @@ -1345,7 +1339,7 @@ mps_routedatapoint_w(FILE *mps_file, int mps_ver, const waypoint *rtewpt) else { hdr[0] = 1; fwrite(hdr, 1 , 1, mps_file); - le_fwrite64(&mps_altitude, 8 , 1, mps_file); + le_fwrite_double(mps_altitude, mps_file ); } /* output end point 2 */ @@ -1364,7 +1358,7 @@ mps_routedatapoint_w(FILE *mps_file, int mps_ver, const waypoint *rtewpt) else { hdr[0] = 1; fwrite(hdr, 1 , 1, mps_file); - le_fwrite64(&mps_altitude, 8 , 1, mps_file); + le_fwrite_double(mps_altitude, mps_file); } if (rtewpt->latitude > prevRouteWpt->latitude) { @@ -1409,7 +1403,7 @@ mps_routedatapoint_w(FILE *mps_file, int mps_ver, const waypoint *rtewpt) else { hdr[0] = 1; fwrite(hdr, 1 , 1, mps_file); - le_fwrite64(&maxalt, 8 , 1, mps_file); + le_fwrite_double(maxalt, mps_file); } /* output min coords of the link */ @@ -1425,7 +1419,7 @@ mps_routedatapoint_w(FILE *mps_file, int mps_ver, const waypoint *rtewpt) else { hdr[0] = 1; fwrite(hdr, 1 , 1, mps_file); - le_fwrite64(&minalt, 8 , 1, mps_file); + le_fwrite_double(minalt, mps_file ); } } @@ -1560,11 +1554,11 @@ mps_track_r(FILE *mps_file, int mps_ver, route_head **trk) fread(tbuf, 1, 1, mps_file); /* altitude validity */ if (tbuf[0] == 1) { - le_fread64(&mps_altitude,sizeof(mps_altitude),1,mps_file); + mps_altitude = le_fread_double( mps_file ); } else { mps_altitude = unknown_alt; - le_fread64(tbuf,sizeof(mps_altitude),1, mps_file); + fseek( mps_file, 8, SEEK_CUR ); } fread(tbuf, 1, 1, mps_file); /* date/time validity */ @@ -1577,11 +1571,11 @@ mps_track_r(FILE *mps_file, int mps_ver, route_head **trk) fread(tbuf, 1, 1, mps_file); /* depth validity */ if (tbuf[0] == 1) { - le_fread64(&mps_depth,sizeof(mps_depth),1,mps_file); + mps_depth = le_fread_double(mps_file ); } else { mps_depth = unknown_alt; - le_fread64(tbuf,sizeof(mps_depth),1, mps_file); + fseek( mps_file, 8, SEEK_CUR ); } thisWaypoint = waypt_new(); @@ -1706,7 +1700,7 @@ mps_trackdatapoint_w(FILE *mps_file, int mps_ver, const waypoint *wpt) else { hdr[0] = 1; fwrite(hdr, 1 , 1, mps_file); - le_fwrite64(&mps_altitude, 8 , 1, mps_file); + le_fwrite_double(mps_altitude, mps_file); } if (t > 0) { /* a valid time is assumed to > 0 */ @@ -1725,7 +1719,7 @@ mps_trackdatapoint_w(FILE *mps_file, int mps_ver, const waypoint *wpt) else { hdr[0] = 1; fwrite(hdr, 1 , 1, mps_file); - le_fwrite64(&mps_depth, 8 , 1, mps_file); + le_fwrite_double(mps_depth, mps_file ); } } diff --git a/psp.c b/psp.c index 76298718e..3970faaaf 100644 --- a/psp.c +++ b/psp.c @@ -53,20 +53,15 @@ static double psp_fread_double(FILE *fp) { unsigned char buf[8]; - unsigned char sbuf[8]; - psp_fread(buf, 1, 8, fp); - le_read64(sbuf, buf); - return *(double *) sbuf; + return le_read_double(buf); } static void psp_fwrite_double(double x, FILE *fp) { - unsigned char *cptr = (unsigned char *)&x; unsigned char cbuf[8]; - - le_read64(cbuf, cptr); + le_write_double(cbuf,x); fwrite(cbuf, 8, 1, fp); } diff --git a/saroute.c b/saroute.c index 925f42c11..cd9648cf6 100644 --- a/saroute.c +++ b/saroute.c @@ -323,7 +323,7 @@ my_read(void) } if ( timesynth ) { - le_read64( &seglen, + seglen = le_read_double( record + 2 + stringlen + 0x08 ); starttime = le_read32((unsigned long *) (record + 2 + stringlen + 0x30 )); diff --git a/tpg.c b/tpg.c index f1f7715c9..122fa6488 100644 --- a/tpg.c +++ b/tpg.c @@ -65,20 +65,15 @@ static double tpg_fread_double(FILE *fp) { unsigned char buf[8]; - unsigned char sbuf[8]; - tpg_fread(buf, 1, 8, fp); - le_read64(sbuf, buf); - return *(double *)sbuf; + return le_read_double(buf); } static void tpg_fwrite_double(double x, FILE *fp) { - unsigned char *cptr = (unsigned char *)&x; unsigned char cbuf[8]; - - le_read64(cbuf, cptr); + le_write_double(cbuf,x); fwrite(cbuf, 8, 1, fp); } diff --git a/tpo.c b/tpo.c index 8a3fed2b3..0e3c1a22a 100644 --- a/tpo.c +++ b/tpo.c @@ -145,20 +145,15 @@ static double tpo_fread_double(FILE *fp) { unsigned char buf[8]; - unsigned char sbuf[8]; - tpo_fread(buf, 1, 8, fp); - le_read64(sbuf, buf); - return *(double *)sbuf; + return le_read_double(buf); } static void tpo_fwrite_double(double x, FILE *fp) { - unsigned char *cptr = (unsigned char *)&x; unsigned char cbuf[8]; - - le_read64(cbuf, cptr); + le_write_double(cbuf,x); fwrite(cbuf, 8, 1, fp); } diff --git a/util.c b/util.c index 282ed20c2..0a0356d7c 100644 --- a/util.c +++ b/util.c @@ -813,38 +813,65 @@ static int doswap() } double -pdb_read_double(void* ptr) +endian_read_double(void* ptr, int read_le) { double ret; char r[8]; + void *p; int i; doswap(); /* make sure i_am_little_endian is initialized */ - for (i = 0; i < 8; i++) - { - int j = (i_am_little_endian)?(7-i):i; - r[i] = ((char*)ptr)[j]; + if ( i_am_little_endian == read_le ) { + p = ptr; + } + else { + for (i = 0; i < 8; i++) + { + r[i] = ((char*)ptr)[7-i]; + } + p = r; } - memcpy(&ret, r, 8); + + memcpy(&ret, p, 8); return ret; } void -pdb_write_double(void* ptr, double d) +endian_write_double(void* ptr, double d, int write_le) { - char r[8]; + char *r = (char *)(void *)&d; int i; char *optr = ptr; - memcpy(r, &d, 8); doswap(); /* make sure i_am_little_endian is initialized */ - for (i = 0; i < 8; i++) - { - int j = (i_am_little_endian)?(7-i):i; - *optr++ = r[j]; + if ( i_am_little_endian == write_le ) { + memcpy( ptr, &d, 8); + } + else { + for (i = 0; i < 8; i++) + { + *optr++ = r[7-i]; + } } - return; } +double +pdb_read_double( void *ptr ) {return endian_read_double(ptr, 0);} + +double +le_read_double( void *ptr ) {return endian_read_double(ptr,1);} + +double +be_read_double( void *ptr ) {return endian_read_double(ptr,0);} + +void +pdb_write_double( void *ptr, double d ) {endian_write_double(ptr,d,0);} + +void +le_write_double( void *ptr, double d ) {endian_write_double(ptr,d,1);} + +void +be_write_double( void *ptr, double d ) {endian_write_double(ptr,d,0);} + /* Magellan and PCX formats use this DDMM.mm format */ double ddmm2degrees(double pcx_val) { double minutes; diff --git a/vitosmt.c b/vitosmt.c index 3ca048651..4a9bedf64 100644 --- a/vitosmt.c +++ b/vitosmt.c @@ -53,11 +53,8 @@ static double ReadDouble(FILE * f) { unsigned char buffer[8] = "\0\0\0\0\0\0\0\0"; - double result=0; - fread(buffer, sizeof (buffer), 1, f); - le_read64(&result,buffer); - return result; + return le_read_double(buffer ); } @@ -75,11 +72,8 @@ static void WriteDouble(void* ptr, double d) { unsigned char result[8]="\0\0\0\0\0\0\0\0"; - - le_read64(result, &d); + le_write_double(result,d); memcpy(ptr, result, 8); - - return; } -- 2.30.2